Skip to content

Conversation

@KJ7LNW
Copy link
Contributor

@KJ7LNW KJ7LNW commented May 17, 2025

Context

This PR addresses the need to standardize explicit type coercion throughout the codebase to enhance code clarity and prevent ambiguity related to falsy value evaluation.

Implementation

  • Enabled the 'no-implicit-coercion' ESLint rule to enforce explicit type conversions
  • Added detailed guidance in .roo/rules/no-implicit-coercion.md explaining how to properly handle type coercion
  • Fixed numerous instances of implicit coercion across the codebase, replacing them with explicit, context-aware comparisons
  • Addressed unused variables by removing them rather than prefixing with underscores
  • Updated the lint:extension script to treat warnings as errors

These changes improve code quality by:

  • Clarifying code intent through explicit type checks
  • Reducing ambiguity in conditional logic
  • Preventing potential bugs from overly general truthiness checks
  • Establishing consistent standards for handling type coercion

How to Test

  • Run ESLint to verify no implicit coercion errors remain
  • Review the code changes to ensure they follow the guidelines in no-implicit-coercion.md
  • Verify that the application functions correctly with the updated conditional checks

Get in Touch

Discord: KJ7LNW

Fixes #3692


Important

Enforces explicit type coercion and addresses unused variables across the codebase, enhancing code clarity and consistency.

  • ESLint Configuration:
    • Enabled no-implicit-coercion rule in .eslintrc.json and webview-ui/.eslintrc.json.
    • Added no-empty rule to enforce non-empty block statements.
  • Documentation:
    • Added eslint-no-implicit-coercion.md and eslint-no-empty.md for guidance on handling these rules.
  • Code Changes:
    • Replaced implicit coercion with explicit checks in modes.test.ts, subtasks.test.ts, task.test.ts, vscode-lm.ts, ContextProxy.ts, importExport.ts, executeCommandTool.ts, writeToFileTool.ts, ClineProvider.ts, webviewMessageHandler.ts, open-file.ts, ExecaTerminalProcess.ts, ShadowCheckpointService.ts, excludes.ts, combineApiRequests.ts, modes.ts, tts.ts, AutoApproveMenu.tsx, ChatView.tsx, TaskActions.tsx, TaskHeader.tsx, HistoryPreview.tsx, HistoryView.tsx, AutoApproveToggle.tsx, ThinkingBudget.tsx, Anthropic.tsx, Gemini.tsx, OpenAI.tsx, OpenAICompatible.tsx, OpenRouter.tsx, ChatInput.tsx, ChatMessages.tsx, useOpenRouterKeyInfo.ts, useRequestyKeyInfo.ts.
    • Removed unused variables instead of prefixing with underscores.
  • Linting Script:
    • Updated lint:extension script in package.json to treat warnings as errors.

This description was created by Ellipsis for 4bec6418b287c477b863054e4fa033ed0762930f. You can customize this summary. It will automatically update as commits are pushed.

@changeset-bot
Copy link

changeset-bot bot commented May 17, 2025

⚠️ No Changeset found

Latest commit: b3489f271d6369a5da85c0db9a8fc7ed836a2e34

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels May 17, 2025
@KJ7LNW KJ7LNW changed the title feat: Standardize explicit type coercion to enhance code clarity lint: Standardize explicit type coercion to enhance code clarity May 17, 2025
@KJ7LNW KJ7LNW marked this pull request as draft May 20, 2025 02:01
@hannesrudolph hannesrudolph moved this from New to PR [Draft/WIP] in Roo Code Roadmap May 20, 2025
@KJ7LNW KJ7LNW force-pushed the feat-no-implicit-coercion branch 2 times, most recently from 2b91975 to 4bec641 Compare May 21, 2025 00:33
@KJ7LNW KJ7LNW marked this pull request as ready for review May 21, 2025 00:50
@hannesrudolph hannesrudolph moved this from PR [Draft / In Progress] to TEMP in Roo Code Roadmap May 26, 2025
@daniel-lxs daniel-lxs moved this from TEMP to PR [Needs Review] in Roo Code Roadmap May 26, 2025
@hannesrudolph hannesrudolph moved this from PR [Needs Review] to TEMP in Roo Code Roadmap May 26, 2025
@hannesrudolph hannesrudolph moved this from TEMP to PR [Needs Review] in Roo Code Roadmap May 26, 2025
@daniel-lxs
Copy link
Member

Hey @KJ7LNW, Do you think you can rebase this to properly review it and potentially merge it?

Thank you!

@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Changes Requested] in Roo Code Roadmap Jun 2, 2025
@KJ7LNW
Copy link
Contributor Author

KJ7LNW commented Jun 2, 2025

Hey @KJ7LNW, Do you think you can rebase this to properly review it and potentially merge it?

Thank you!

I can do that. rebase is pretty simple: instead of rebasing, I delete the commit that apply is the change and have Roo re-apply them because they are not that many changes.

Eric Wheeler added 3 commits June 2, 2025 14:35
Add no-implicit-coercion and no-empty rules to improve code quality across the codebase:

- no-implicit-coercion: Enforces explicit, context-aware type comparisons to address
  ambiguity issues with falsy values and improve code clarity
- no-empty: Requires all code blocks to have meaningful content, preventing silent
  error suppression and improving code readability

This change includes:
- Adding detailed documentation in .roo/rules/eslint-no-implicit-coercion.md with
  specific guidance on proper type checking patterns
- Adding documentation for no-unused-vars rule enforcement in .roo/rules/eslint-no-unused-vars.md
- Explicitly adding both rules as "error" in both root and webview-ui configs
- Setting --max-warnings=0 in lint scripts to ensure strict enforcement

These changes ensure developers use explicit type checks and properly document
empty blocks, particularly important for distinguishing between null, undefined,
and empty strings in conditional logic.

Fixes: #3692
Signed-off-by: Eric Wheeler <[email protected]>
Enforce the `no-useless-catch` ESLint rule across the codebase.
This rule disallows `catch` clauses that only rethrow the caught error,
which provides no additional value and can make debugging harder.

This change adds the rule as an "error" to:
- The root ESLint configuration ([](/.eslintrc.json))
- The webview UI ESLint configuration ([](webview-ui/.eslintrc.json))

This ensures that all `catch` blocks either handle the error meaningfully
or are omitted if the error should propagate.

Signed-off-by: Eric Wheeler <[email protected]>
Replace implicit boolean coercions (git diff --staged | catvariable) with explicit type-aware checks
to satisfy the no-implicit-coercion ESLint rule. Also add debug logging to
empty blocks to satisfy the no-empty rule.

- Use type-specific checks (typeof x === 'string') for string variables
- Use explicit comparisons (x === true) for boolean variables
- Remove redundant null checks for variables that can't be null
- Add console.debug statements to empty catch blocks

Signed-off-by: Eric Wheeler <[email protected]>
Refine guidance on leveraging TypeScript types for explicit boolean checks.
Clarify that redundant checks for conditions already guaranteed by type
definitions (e.g., null checks for `SomeType \| undefined`, or undefined
checks for non-optional members) should be omitted.

Signed-off-by: Eric Wheeler <[email protected]>
@KJ7LNW KJ7LNW force-pushed the feat-no-implicit-coercion branch from b3e20d6 to f824807 Compare June 2, 2025 22:56
@KJ7LNW
Copy link
Contributor Author

KJ7LNW commented Jun 2, 2025

@daniel-lxs

tests are currently passing it and there are no merge conflicts because I just cleaned this all up, as well as validated the correctness of each change:

Please merge this ASAP to avoid duplicating that effort in the future, because eslint will handle all of these issues going forward.

@daniel-lxs daniel-lxs moved this from PR [Changes Requested] to PR [Needs Review] in Roo Code Roadmap Jun 3, 2025
@daniel-lxs
Copy link
Member

Looks good to me, great effort.

@KJ7LNW
Copy link
Contributor Author

KJ7LNW commented Jun 3, 2025

Looks good to me, great effort.

@mrubens "linting" PRs tend to diverge quickly so if you can review and merge this before other pull requests that might conflict, then I would appreciate it!

Copy link
Collaborator

@mrubens mrubens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I want to be really judicious with the linting errors that we add to the project and only add ones that clearly impact the correctness of the code. I don’t want to constrain the LLM output or add to the system prompt unnecessarily. I’m sorry about any conflicts this will add, but I think we should discuss more whether these rules are worth it.

@KJ7LNW
Copy link
Contributor Author

KJ7LNW commented Jun 3, 2025

I think we should discuss more whether these rules are worth it

Certainly! I would not have spent the time on this without good reason:

In short, failing to differentiate between empty (ie, 0 or "") and undefined will introduce bugs:

  • empty is often valid
  • undefined often indicates a case that may require special handling

This PR was motivated by hitting bugs around if (foo) checks, typically regarding regular expressions, and it is a good practice this everywhere to force developers to write what they mean. Most importantly, handling error cases gracefully with intention makes for a solid application.

Since there are so many developers of varying experience that rely on AI to make changes to Roo, a small amount of eslint can go a long way to enforce code quality and prevent bugs---especially subtle issues like empty vs undefined that are likely to be overlooked by review.

Here is one example that I have hit in the past:

When I was originally troubleshooting vscode terminal shell integration escape sequence issues, AI would consistently do something like the following, even though I told it numerous times that an "empty" match is valid:

// this cannot differentiate between an empty match and no match:
matches = matches(/$prefix(.*)$/)
if (matches[0])  // fails in "" but "" != undefined
  success 
else 
  failure 

If we add these eslint configurations, then it would force Roo developers (and the model) to make determinations more accurately instead of just assuming "truthy/falsy" evaluation to be sufficient.

I don’t want to constrain the LLM output

What do you mean by constrain the output? This does not prevent any particular implementation, it just requires developers to enforce type-safety.

or add to the system prompt unnecessarily.

Some instruction to the model specific to Roo-programming guidelines is required so that the model does not simply do change if (foo) to if (Boolean(foo)), which completely bypasses the intended result of code quality; eslint (inappropriately) recommends Boolean(foo) in the error as the suggested fix, so the model needs some guidance.

These .roo/rules/eslint-* files only affect development in Roo, and they are pretty concise. I used the included system instructions for solving the above eslint issues, and they worked quite well, so I included them with the pull request. We could shorten them if necessary, but they will get cached with the system instructions and would only lead to better programming habits for new and existing developers into the future.

@daniel-lxs daniel-lxs moved this from PR [Needs Review] to PR [Needs Prelim Review] in Roo Code Roadmap Jun 20, 2025
@daniel-lxs
Copy link
Member

Moving to draft temporarily

@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Draft / In Progress] in Roo Code Roadmap Jun 21, 2025
@daniel-lxs daniel-lxs marked this pull request as draft June 21, 2025 13:24
@hannesrudolph
Copy link
Collaborator

stale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PR - Draft / In Progress size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Standardize Explicit Type Coercion to Enhance Code Clarity and Prevent Ambiguity

4 participants